package com.chensf.studydemo.common

import android.os.Bundle
import android.os.Environment
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chensf.studydemo.R
import com.chensf.studydemo.common.query.PinYin
import kotlinx.android.synthetic.main.fragment_file_browse.*
import kotlinx.android.synthetic.main.view_file_item.view.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.File
import java.util.*
import kotlin.Comparator

class FileBrowseDemo : Fragment() {
    private lateinit var adapter: FileAdapter
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_file_browse, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        recyclerView.layoutManager = LinearLayoutManager(requireContext())
        adapter = FileAdapter()
        recyclerView.adapter = adapter
        lifecycleScope.launch(Dispatchers.Main){
            val fileList = withContext(Dispatchers.IO){
                val root = Environment.getExternalStorageDirectory()
                val files = root.listFiles()
                val list = mutableListOf<File>()
                files?.let {
                    list.addAll(it)
                }
                list
            }
            val filterList = fileList.filter {
                !it.isHidden
            }
            Collections.sort(filterList, object : Comparator<File> {
                override fun compare(o1: File, o2: File): Int {
                    if (o1.isDirectory && o2.isFile) return -1
                    return if (o1.isFile && o2.isDirectory) 1 else {
                        Log.e("foo", "o1:(${o1.name}-${PinYin.getPinYin(o1.name)})   o2:(${o2.name}-${PinYin.getPinYin(o2.name)})" )
                        PinYin.getPinYin(o1.name).toLowerCase(Locale.getDefault()).compareTo(PinYin.getPinYin(o2.name).toLowerCase(Locale.getDefault()))
                    }
                }
            })
            adapter.setData(filterList)
        }
    }
}

class FileAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    private val data = mutableListOf<File>()

    fun setData(list: List<File>){
        data.clear()
        data.addAll(list)
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return if(viewType==1){
            DirHolder(
                LayoutInflater.from(parent.context).inflate(
                    R.layout.view_file_item,
                    parent,
                    false
                )
            )
        }else{
            FileHolder(
                LayoutInflater.from(parent.context).inflate(
                    R.layout.view_file_item,
                    parent,
                    false
                )
            )
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val item = data[position]
        if(holder is FileHolder){
            holder.fileName.text = "文件-${item.name}"
        }else if(holder is DirHolder){
            holder.fileName.text = "目录-${item.name}"
        }
    }

    override fun getItemCount(): Int {
        return data.size
    }

    override fun getItemViewType(position: Int): Int {
        return if (data[position].isDirectory) {
            1
        } else {
            2
        }
    }

}

class FileHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
    val icon:ImageView = itemView.icon
    val fileName:TextView = itemView.fileName
}
class DirHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
    val icon:ImageView = itemView.icon
    val fileName:TextView = itemView.fileName
}